package aplicacion;

import java.io.File;
import java.util.ArrayList;

import dataType.Coordenadas3D;
import dataType.DataFlowers;
import figuras.PuntoVector;

public class Controlador {


	private DataFlowers dataFlowers;
	
	public Controlador() {
		this.crearDataFlowers();
		this.creaTemp();
	}
		
	public void creaTemp(){
		
		File tmp = new File("resources" + File.separator + "temp");
		tmp.mkdir();		
	}
	
	public void borraTmp(){
		
		File tmp = new File("resources" + File.separator + "temp");
		borraFiles(tmp);
		tmp.delete();
	}
	
	private void borraFiles(File file){
		
		if(file.isDirectory()){
			File[] lista = file.listFiles();
			for(int i=0;i<lista.length;i++)
				borraFiles(lista[i]);
			file.delete();
		}
		else
			file.delete();
		
	}
	
	public void elementoTerminado(){
		
	}
	
	public void crearDataFlowers(){
		dataFlowers = new DataFlowers(4);
	}
		
	public DataFlowers getDataFlowers() {
		return dataFlowers;
	}

	public void setDataFlowers(DataFlowers dataFlowers) {
		this.dataFlowers = dataFlowers;
	}

	public int[] openLibrary(PuntoVector mouse) {

		int [] result = new int[2];
		
		for(int i=0;i<4;i++){
			ArrayList<PuntoVector> lista = dataFlowers.getElementos2D(i);
			for(int j=0;j<lista.size();j++){
				PuntoVector point = lista.get(j);
				if(point.distancia(mouse)<=dataFlowers.getTamanno(i)+3){
					result[0] = i;
					result[1] = j;
					return result;
				}
			}
		}
		return null;
	}

	public void insertaFileReceptaculo(String file){
		dataFlowers.setReceptaculoFile(file);
	}
	
	public void rellenaFileDataFlower(int circulo, int elemento, String name) {

		Coordenadas3D c3d = dataFlowers.getElementos3D(circulo).get(elemento);
		c3d.setFile(name);
	}
	
	public void rellenaFilenames(int c, String file){
		dataFlowers.rellenaFilenames(c, file);
	}
	
	public int dameNumElement2D(int c){
		return dataFlowers.getElementos2D(c).size();
	}
	
	public void borraFilesDataFlower(){
		dataFlowers.borraFiles();
	}
}
